package com.exercise.calculatorapp;

import android.graphics.Typeface;
import android.os.Bundle;

import com.exercise.calculatorapp.enums.Rid;
import com.exercise.calculatorapp.util.ExpressionParser;
import com.google.android.material.snackbar.Snackbar;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import android.view.View;

import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import androidx.navigation.ui.NavigationUI;

import com.exercise.calculatorapp.databinding.ActivityMainBinding;

import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.TextView;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    private String fristNum = "";
    private String operator = "";
    private String secondNum = "";
    private String result = "";
    private String showText = "";

    private TextView tv_input;
    private TextView tv_result;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        tv_input = findViewById(R.id.tv_input);
        tv_result = findViewById(R.id.tv_result);
        findViewById(R.id.btn_cancel).setOnClickListener(this);
        findViewById(R.id.btn_divide).setOnClickListener(this);
        findViewById(R.id.btn_multiply).setOnClickListener(this);
        findViewById(R.id.btn_clear).setOnClickListener(this);
        findViewById(R.id.btn_nine).setOnClickListener(this);
        findViewById(R.id.btn_eight).setOnClickListener(this);
        findViewById(R.id.btn_seven).setOnClickListener(this);
        findViewById(R.id.btn_plus).setOnClickListener(this);
        findViewById(R.id.btn_six).setOnClickListener(this);
        findViewById(R.id.btn_five).setOnClickListener(this);
        findViewById(R.id.btn_four).setOnClickListener(this);
        findViewById(R.id.btn_minus).setOnClickListener(this);
        findViewById(R.id.btn_three).setOnClickListener(this);
        findViewById(R.id.btn_two).setOnClickListener(this);
        findViewById(R.id.btn_one).setOnClickListener(this);
        findViewById(R.id.btn_radical).setOnClickListener(this);
        findViewById(R.id.btn_reciprocal).setOnClickListener(this);
        findViewById(R.id.btn_zero).setOnClickListener(this);
        findViewById(R.id.btn_dot).setOnClickListener(this);
        findViewById(R.id.btn_equal).setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        String inputText = "";
        if (view.getId() == R.id.btn_radical) {
            inputText += "√";
        }else {
            inputText = ((Button)view).getText().toString();
        }
        switch (view.getId()) {
            // 清除和退格
            case R.id.btn_cancel:
                break;
            case R.id.btn_clear:
                this.clear();
                break;
            // 运算
            case R.id.btn_plus:
            case R.id.btn_minus:
            case R.id.btn_multiply:
            case R.id.btn_divide:
                if (!operator.isEmpty() && secondNum.isEmpty()){
                    break;
                }
                operator = ((Button)view).getText().toString();
                refreshView(showText + inputText);
                break;
            // 等于
            case R.id.btn_equal:
                if (fristNum.isEmpty()) {
                    break;
                }else if(!operator.isEmpty() && secondNum.isEmpty()) {
                    refreshView(fristNum);
                }
                this.getResult(R.id.btn_equal,showText);
                break;
            // 倒数
            case R.id.btn_reciprocal:

                break;
            default:
                if (operator.isEmpty()) {
                    fristNum += inputText;
                }else {
                    secondNum += inputText;
                }
                if (inputText.equals("0") && !inputText.equals(".")) {
                    refreshView(showText);
                }else {
                    refreshView(showText + inputText);
                }
                if (!operator.isEmpty()) {
                    getResult(null,showText);
                }
                break;
        }

    }

    // 拼接字符
    private void refreshView (String text) {
        showText = text;
        tv_input.setText(showText);
    }

    // 清空屏幕输入
    private void clear () {
        fristNum = "";
        operator = "";
        secondNum="";
        showText = "";
        result = "";
        tv_input.setText(showText);
    }

    // 得出结果
    private void getResult (Integer rid,String text) {
        text = text.replace("x", "*");
        text = text.replace("÷", "/");
        ExpressionParser expressionParser = new ExpressionParser(text);
        Double parse = expressionParser.parse();
        // 检查结果是否为整数
        if (parse != null) {
            if (parse == Math.floor(parse) && !Double.isInfinite(parse)) {
                // 如果是整数，转换为 Integer
                Integer intResult = parse.intValue();
                tv_result.setText(intResult.toString());
            } else {
                // 如果是浮点数，直接显示
                tv_result.setText(parse.toString());
            }
        }
        // 用户输入了等于号
        if (!Objects.isNull(rid) && R.id.btn_equal == rid) {
            result = parse.toString();
            tv_result.setText("");
            tv_input.setText(result);
            saveResult(result);
        }
    }

    // 将计算结果保留
    private void saveResult (String result) {
        fristNum = result;
        refreshView(result);
    }








}